package com.esteel.settlement.pa.service.impl;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.esteel.settlement.entity.TbSettFund;
import com.esteel.settlement.entity.TbSettFundChg;
import com.esteel.settlement.entity.TbSettPay;
import com.esteel.settlement.pa.packet.Answer;
import com.esteel.settlement.pa.packet.BankReviewNoticeBP1326;
import com.esteel.settlement.pa.packet.PaPacket;
import com.esteel.settlement.pa.packet.RspPB1326;
import com.esteel.settlement.pa.service.PacketProcesser;
import com.esteel.settlement.service.SettFundChgService;
import com.esteel.settlement.service.SettFundService;
import com.esteel.settlement.service.SettPayService;

/**
 * 银行复核通知的处理
 * @author zhangxiuzhi
 *
 */

@Service
public class BankReviewNoticeBP1326Processer  extends PacketProcesser<BankReviewNoticeBP1326, RspPB1326>  {

	@Autowired
	private SettPayService settPayService;
	
	@Autowired
	private SettFundService settFundService;
	
	@Autowired
	private SettFundChgService settFundChgService;
	
	@Override
	@Transactional
	public RspPB1326 execute(BankReviewNoticeBP1326 packet) {
		//修改支付记录表信息

		RspPB1326 rsp= new RspPB1326();
		rsp.setTranFunc("1326");
		rsp.setServType(PaPacket.RESPONSE);
		rsp.setTransDate(new Date());
		rsp.setQydm(packet.getQydm());
		rsp.setThirdLogNo("1326_"+new Date().getTime());
		
		TbSettPay settPay = settPayService.findByPaySerialNo(packet.getPaySerialNo());
		
		if (settPay==null){
			rsp.setRspCode("000001");
			rsp.setRspMsg("支付订单不存在");
			return rsp;
		}
		
		settPay.setCheckResult(packet.getCheckResult());
		settPay.setCheckCustAcctId(packet.getCustAcctId());
		settPay.setCheckThirdCustId(packet.getThirdCustId());
		if (packet.getCheckResult().equals("Y")){
			settPay.setStatus(2);
		}else if (packet.getCheckResult().equals("N")){
			settPay.setStatus(3);
		}
		settPay.setCheckTime(packet.getTransDate());
		settPayService.save(settPay);
		
		//修改账户余额等信息
		if (packet.getCheckResult().equals("Y")){
			this.payComplete(settPay,rsp,TbSettFundChg.LinkType.PAYCONFIRM);
		}
	
		rsp.setThirdLogNo("1326_"+settPay.getPayId());

		rsp.setRspCode(PaPacket.SUCCESS);
		return rsp;
	}

	@Override
	protected String getTranFunc() {
		return "1326";
	}

	/**
	 *  支付订单完成的处理过程
	 * @param settPay 强制支付订单内容
	 * @param rsp 支付返回报文
	 * @param ChgType 记账标志
	 */
	public void payComplete(TbSettPay settPay ,Answer rsp,int ChgType){
			
		//修改账户余额等信息
			//付款方账户信息
			TbSettFund outFund = settFundService.findByCustAcctId(settPay.getOutCustAcctId());
			TbSettFund inFund = settFundService.findByCustAcctId(settPay.getInCustAcctId());
			TbSettFund feeFund = settFundService.findCharges();
			
			double tranAmount = settPay.getTranAmount();
			double handFee = settPay.getHandFee();
			
			//减少付款账户的总金额和冻结余额
			outFund.setTotalAmount(outFund.getTotalAmount()-tranAmount-handFee);
			outFund.setTotalFreezeAmount(outFund.getTotalFreezeAmount()-tranAmount-handFee);
			
			//增加收款账户的总金额和冻结金额
			inFund.setTotalAmount(inFund.getTotalAmount()+tranAmount);
			inFund.setTotalFreezeAmount(inFund.getTotalFreezeAmount()+tranAmount);
			
			//增加手续费账户的总余额和可用金额
			feeFund.setTotalAmount(feeFund.getTotalAmount()+handFee);
			feeFund.setTotalBalance(feeFund.getTotalBalance()+handFee);
			
			settFundService.save(outFund);
			settFundService.save(inFund);
			settFundService.save(feeFund);
			
			// 记录转出方资金变动情况
			TbSettFundChg outFundChg = new TbSettFundChg();
			BeanUtils.copyProperties(outFund.getId(), outFundChg);
			BeanUtils.copyProperties(outFund, outFundChg);
			// 设置资金变动类型
			outFundChg.setLinkType(ChgType);
			outFundChg.setLinkKey(settPay.getPayId());
			outFundChg.setTransDate(rsp.getTransDate());
			//发送交易请求后 交易方资金被冻结
			outFundChg.setChgTotalFreezeAmount(-(tranAmount+handFee));
			outFundChg.setChgTotalAmount(-(tranAmount+handFee));
			
			settFundChgService.save(outFundChg);
			
			// 记录转入方方资金变动情况
			TbSettFundChg inFundChg = new TbSettFundChg();
			BeanUtils.copyProperties(inFund.getId(), inFundChg);
			BeanUtils.copyProperties(inFund, inFundChg);
			//设置资金变动类型
			inFundChg.setLinkType(ChgType);
			inFundChg.setLinkKey(settPay.getPayId());
			inFundChg.setTransDate(rsp.getTransDate());
			//发送交易请求后 交易方资金被冻结
			inFundChg.setChgTotalFreezeAmount(tranAmount);
			inFundChg.setChgTotalAmount(tranAmount);
			settFundChgService.save(inFundChg);
			
			//记录手续费子账户 资金变动情况
			TbSettFundChg feeFundChg = new TbSettFundChg();
			BeanUtils.copyProperties(feeFund.getId(), feeFundChg);
			BeanUtils.copyProperties(feeFund, feeFundChg);
			//设置资金变动类型
			//TODO 确认支付类型
			feeFundChg.setLinkType(TbSettFundChg.LinkType.HANDFEEIN);
			feeFundChg.setLinkKey(settPay.getPayId());
			feeFundChg.setTransDate(rsp.getTransDate());
			//发送交易请求后 交易方资金被冻结
			feeFundChg.setChgTotalBalance(handFee);
			feeFundChg.setChgTotalAmount(handFee);
			settFundChgService.save(feeFundChg);
			
			chgOrderStatus(settPay);

		
	}
	
	/**
	 * 修改订单状态
	 * @param packet
	 */
	public void chgOrderStatus(TbSettPay settPay){
		String orderNo = settPay.getThirdHtId();
		String paySerialNo = settPay.getPaySerialNo();
		//XXX 处理逻辑不合理
		if (paySerialNo.endsWith("_00")){
			//处理订金
			settPayService.payDeposit(orderNo);
		}else if (paySerialNo.endsWith("_99")){
			//处理尾款
			settPayService.payFinal(orderNo);
		}else{
//			String payNo = paySerialNo.substring(paySerialNo.lastIndexOf("_")+1);
			
//			if (settPay.getQuantity()==0){
//				//buy_order_status=4
//				//sale_order_status=4
//				//buy_opertaror=付货款
//						//sale_operator=''
//						//order_status_type=2
//				
////				* 插入log 2 ‘付货款’ 
//				settPayService.payment(orderNo,0);
//			}else{
				//处理货款
				settPayService.payment(orderNo,settPay.getQuantity());
/*
					//buy_order_status=5
					//sale_order_status=5
					//buy_opertaror=''
					//sale_operator='上传放货单'
					//order_status_type=3
					 * 
					 * 插入log 2 ‘付货款’ 
				}
				*/
//			}
			
		
		}
	}
}
